// pages/shop/index/index.js
var API = require("../../../config/api.js");
var UTILS = require("../../../utils/util.js");

var APP = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: -1,
    shopInfo: {},
    productList: [],
    RESOURCE_ROOT: "",
    nullPalacehold: "这家伙很懒，什么也没填",
    shopActive: [],
    cartCount: 0,
    imageURL: '//img.yzcdn.cn/upload_files/2017/07/02/af5b9f44deaeb68000d7e4a711160c53.jpg'
  },
  /**
   * 自定义函数
   */
  onChange: function () {

  },
  toDeal: function () {
    var shopId = this.data.shopId;
    wx.navigateTo({
      url: '../deal/deal?shopId=' + shopId,
      success: function(res){},
      fail: function() {},
      complete: function() {}
    });
  },
  switchTabs: function (event) {
    var index = event.detail.index;
    var cateId = this.data.shopInfo.categoryList[index].id;
    this.getProductListByCateId(cateId);
  },
  onContentChange(event) {
    const { key } = event.currentTarget.dataset;
    this.setData({
      [key]: event.detail
    });
  },
  onClickLeft: function () {
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
      success: function(res){
        
      }
    });
  },
  toCartPage: function () {
    wx.switchTab({
      url: '/pages/cart/cart'
    });
  },
  getShopInfo: function (shopId) {
    if (shopId < 0) return;
    var _this = this;
    var url = API.GET_SHOP_INFO_URL + "/" + shopId;

    UTILS.httpGet(url, {}, function(data) {
      _this.setData({
        shopInfo: data.result
      });
    });
  },
  getProductListByCateId: function (cateId) {
    var _this = this;
    var shopId = this.data.shopId;
    var url = API.GET_PRODUCT_LIST_URL + "/" + shopId + "/" + cateId + "/1/10";

    UTILS.httpGet(url, {}, function(data) {
      _this.setData({
        productList: data.result.records
      });
    });
  },
  getCartItemCount: function(shopId) {
    var _this = this;
    var userId = APP.globalData.userInfo.id;
    var url = API.GET_CART_ITEM_COUNT + "/" + userId + "/" + shopId;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };

    UTILS.httpGet(url, header, function (data) {
      _this.setData({
        cartCount: data.result
      });
    });
  },
  addToCart: function (event) {
    var _this = this;
    var dataset = event.currentTarget.dataset;
    var header = {
      Authorization: APP.globalData.token,
      userId: APP.globalData.userInfo.id
    };
    var requestParam = {
      shopId: this.data.shopId,
      userId: APP.globalData.userInfo.id,
      productId: dataset.productid,
      productCount: dataset.count,
      checked: UTILS.CHECKED
    };

    UTILS.httpPost(API.CREATE_CART_ITEM, header, requestParam, function(data) {
      UTILS.showSuccessModel("已经添加到购物车");
      _this.getCartItemCount(_this.data.shopId);
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId,
      RESOURCE_ROOT: API.RESOURCE_ROOT
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getShopInfo(this.data.shopId);
    this.getCartItemCount(this.data.shopId);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})